<?php
/**
 * Flea Site
 *
 * @copyright  Copyright (c) 2008 Movoin Studio (http://movoin.com)
 * @license    Apache License 2.0
 * @version    $Id: Categorysort.php 138 2009-05-25 06:42:38Z movoin $
 */

/**
 * 排序控件
 *
 */
function _ctlCategorysort($name, $args=null)
{
    $opts = array('colkey', 'site', 'column', 'url', 'sorturl');
    $data = FLEA_WebControls::extractAttribs($args, $opts);
    FLEA_WebControls::mergeAttribs($args);

    $parent_id = (int)$_GET['parent_id'];

    $table =& FLEA::getSingleton( 'table_categories' );
    $where = array(
        array( 'col_key', $data['colkey'] ),
        array( 'site_id', (int)$data['site'] ),
        array('parent_id', $parent_id)
        );
    $cate_where = array(
        array( 'col_key', $data['colkey'] ),
        array( 'site_id', (int)$data['site'] ),
        array('parent_id', 0)
        );
    $categories = $table->findAll($cate_where, 'cate_id ASC', null, 'cate_id,name');

    $output = '<script language="JavaScript" src="' . FLEA::getAppInf( 'tpldir' ) . '/img/SortSelect-min.js"></script>';

    $output .= '<form method="post" action="' . $data['url'] . '" id="' . $name . '" name="' . $name . '">';
    $output .= '<h3>' . $data['column'] . '</h3>';
    $output .= '<table>';
    $output .= '<tr>';
    $output .= '<td class="tc">所属分类：';
    $output .= '<select id="parent_id" name="parent_id" style="width:auto;" onchange="document.location=\''.$data['sorturl'].'&parent_id=\'+this.options[this.selectedIndex].value">';
    $output .= '<option value="0">所有一级分类</option>';
    if($categories) {
        foreach($categories as $c) {
            $output .= '<option value="' . $c['cate_id'] . '"';
            if($parent_id==$c['cate_id']) { $output .= ' selected="selected"'; }
            $output .= '>' . $c['name'] . '</option>';
        }
    }
    $output .= '</select>';
    $output .= '</td>';
    $output .= '<td class="tc">查询信息：<input id="search" name="search" type="text" size="25" /><input type="button" onClick="sl.Search()" value="查 询" /></td>';
    $output .= '</tr>';
    $output .= '<tr>';
    $output .= '<td style="width:60%">';
    $output .= '<select id="sort" name="sort" size="15">';

    $rows = $table->findAll($where, 'sort_id ASC, cate_id ASC', null, 'cate_id,name');

    if($rows) {
    foreach($rows as $k => $sortlist) {
        $output .= '<option value="' . $sortlist['cate_id'] . '">' . ($k+1) . '.' . $sortlist['name'] . '</option>';
        }
    }

    $output .= '</select>';
    $output .= '<input type="hidden" name="seqNoList">';
    $output .= '</td>';
    $output .= '<td>';
    $output .= '<input type="button" onClick="sl.fnFirst()" value="第一" /><br />';
    $output .= '<input type="button" onClick="sl.sortUp()" value="上移" /><br />';
    $output .= '<input type="button" onClick="sl.sortDown()" value="下移" /><br />';
    $output .= '<input type="button" onClick="sl.fnEnd()" value="最后" /><br /><br />';
    $output .= '<input type="text" name="jumpNum" size="5" /><input type="button" onClick="sl.jump()" value="跳 转" />';
    $output .= '</td>';
    $output .= '</tr>';
    $output .= '<tr>';
    $output .= '<td colspan="2" class="tc"><input type="submit" onClick="sl.ok()" value="确 定"></td>';
    $output .= '</tr>';
    $output .= '</table>';
    $output .= '</form>';

    $output .= '<script type="text/javascript">';
    $output .= 'var sl = new SortSelect("' . $name . '","sort","search","jumpNum");';
    $output .= '</script>';

    return $output;
}
